﻿namespace Shanda.DevFx.Security.Web.Pages
{
    using Shanda.DevFx.Core;
    using Shanda.DevFx.Security;
    using Shanda.DevFx.Security.Web;
    using Shanda.DevFx.Utils;
    using System;
    using System.Web.UI.WebControls;

    public class GroupUserPage : Shanda.DevFx.Security.Web.BasePage
    {
        protected LinkButton btnAddUserToGroup;
        protected LinkButton btnRemoveUserFromGroup;
        protected DropDownList ddlGroup;
        private string groupNo;
        protected Label lblMessage;
        protected ListBox lbxGroupUser;
        protected ListBox lbxUser;

        private void btnAddUserToGroup_Click(object sender, EventArgs e)
        {
            string str = null;
            for (int i = 0; i < this.lbxUser.get_Items().get_Count(); i++)
            {
                ListItem item = this.lbxUser.get_Items().get_Item(i);
                if (item.get_Selected() && !this.lbxGroupUser.get_Items().Contains(item))
                {
                    IAOPResult result = Securitor.GroupManager.AddUserToGroup(item.get_Value(), this.ddlGroup.get_SelectedValue());
                    if (result.IsFailed)
                    {
                        str = str + "<br>添加用户到组失败：" + result.ResultDescription;
                    }
                    else
                    {
                        this.lbxGroupUser.get_Items().Add(item);
                    }
                }
            }
            this.lblMessage.set_Text(str);
        }

        private void btnRemoveUserFromGroup_Click(object sender, EventArgs e)
        {
            string str = null;
            int num = 0;
            while (num < this.lbxGroupUser.get_Items().get_Count())
            {
                ListItem item = this.lbxGroupUser.get_Items().get_Item(num);
                if (item.get_Selected())
                {
                    IAOPResult result = Securitor.GroupManager.RemoveUserFromGroup(item.get_Value(), this.ddlGroup.get_SelectedValue());
                    if (result.IsFailed)
                    {
                        str = str + "<br>组中移除用户失败：" + result.ResultDescription;
                    }
                    else
                    {
                        this.lbxGroupUser.get_Items().Remove(item);
                    }
                }
                else
                {
                    num++;
                }
            }
            this.lblMessage.set_Text(str);
        }

        private void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.lbxGroupUser.set_DataTextField("UserName");
            this.lbxGroupUser.set_DataValueField("UserNo");
            this.lbxGroupUser.set_DataSource(Securitor.GroupManager.GetGroupUsers(this.ddlGroup.get_SelectedValue()));
            this.lbxGroupUser.DataBind();
        }

        private void InitializeComponent()
        {
            this.ddlGroup.add_SelectedIndexChanged(new EventHandler(this.ddlGroup_SelectedIndexChanged));
            this.btnRemoveUserFromGroup.add_Click(new EventHandler(this.btnRemoveUserFromGroup_Click));
            this.btnAddUserToGroup.add_Click(new EventHandler(this.btnAddUserToGroup_Click));
            base.add_Load(new EventHandler(this.Page_Load));
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
        }

        private void Page_Load(object sender, EventArgs e)
        {
            this.groupNo = base.get_Request().get_QueryString()["GroupNo"];
            Checker.CheckEmptyString("GroupNo", this.groupNo, true);
            if (!base.get_IsPostBack())
            {
                this.ddlGroup.set_DataTextField("IndentTitle");
                this.ddlGroup.set_DataValueField("GroupNo");
                this.ddlGroup.set_DataSource(Securitor.GroupManager.ListItem());
                this.ddlGroup.DataBind();
                this.ddlGroup.set_SelectedValue(this.groupNo);
                this.ddlGroup_SelectedIndexChanged(this, null);
                this.lbxUser.set_DataTextField("UserName");
                this.lbxUser.set_DataValueField("UserNo");
                this.lbxUser.set_DataSource(Securitor.UserManager.ListItem());
                this.lbxUser.DataBind();
            }
        }
    }
}

